<template>
  <!-- 质量模版配置详情表单弹窗 -->
  <el-dialog
          v-model="visibleDialog"
          :title="props.title"
          width="70%"
          draggable
          @close="closeDialog"
  >
  <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
    <el-descriptions-item>
        <template #label>
          <div class="cell-item">配置主键</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.configId}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">排序</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.sort}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">检验类型id</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.checkTypeId}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">检验项目id</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.checkItemId}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">数据版本号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.version}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">备注</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.remarks}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.createTime}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建人</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.createdUserId}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建人名</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.createdUserName}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.lastUpdateTime}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新人</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.lastUpdatedUserId}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新人名</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.lastUpdatedUserName}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">是否启用</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.enabled}}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">删除标识</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{formData.deleted}}
        </div>
      </el-descriptions-item>
  
    </el-descriptions>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160" v-show="!props.isReadonly">
      <el-row>
      <el-col :span="12">
      <el-form-item label="配置主键" prop="configId" label-width="80px">
      <el-input v-model="formData.configId" placeholder="请输入配置主键" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="排序" prop="sort" label-width="80px">
      <el-input v-model="formData.sort" placeholder="请输入排序" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="检验类型id" prop="checkTypeId" label-width="80px">
      <el-input v-model="formData.checkTypeId" placeholder="请输入检验类型id" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="检验项目id" prop="checkItemId" label-width="80px">
      <el-input v-model="formData.checkItemId" placeholder="请输入检验项目id" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="数据版本号" prop="version" label-width="80px">
      <el-input v-model="formData.version" placeholder="请输入数据版本号" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="备注" prop="remarks" label-width="80px">
      <el-input v-model="formData.remarks" placeholder="请输入备注" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建时间" prop="createTime" label-width="80px">
      <el-input v-model="formData.createTime" placeholder="请输入创建时间" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建人" prop="createdUserId" label-width="80px">
      <el-input v-model="formData.createdUserId" placeholder="请输入创建人" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建人名" prop="createdUserName" label-width="80px">
      <el-input v-model="formData.createdUserName" placeholder="请输入创建人名" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新时间" prop="lastUpdateTime" label-width="80px">
      <el-input v-model="formData.lastUpdateTime" placeholder="请输入更新时间" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新人" prop="lastUpdatedUserId" label-width="80px">
      <el-input v-model="formData.lastUpdatedUserId" placeholder="请输入更新人" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新人名" prop="lastUpdatedUserName" label-width="80px">
      <el-input v-model="formData.lastUpdatedUserName" placeholder="请输入更新人名" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="是否启用" prop="enabled" label-width="80px">
      <el-input v-model="formData.enabled" placeholder="请输入是否启用" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="删除标识" prop="deleted" label-width="80px">
      <el-input v-model="formData.deleted" placeholder="请输入删除标识" clearable/>
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import request from "@/utils/request";
  import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
  const dictionaryStore = useDictStoreHook();

  defineOptions({
    name: "QMSQMSREPORTMODELDETAILADDORUPDATE",
    inheritAttrs: false,
  });

  const emit = defineEmits(["reset-table", "change-visible"]);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    title: {
      type: String,
      default: () => {
        return "";
      },
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    isReadonly: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });

  const visibleDialog = ref(false);
  watch(
      () => props.visible,
      (newVal: boolean) => {
        visibleDialog.value = newVal;
        setTimeout(() => {
          formRef.value.clearValidate();
        }, 50);
      }
  );

  const rules = reactive({
    configId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    sort: [
                        ],
    checkTypeId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    checkItemId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    version: [
                        ],
    remarks: [
                        { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" }
                  ],
    createTime: [
                        ],
    createdUserId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    createdUserName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    lastUpdateTime: [
                        ],
    lastUpdatedUserId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    lastUpdatedUserName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    enabled: [
                        ],
    deleted: [
                        ],
  });

  const formRef = ref(ElForm);

  let formData = reactive({
 id: "",  configId: "",  sort: "",  checkTypeId: "",  checkItemId: "",  version: "",  remarks: "",  createTime: "",  createdUserId: "",  createdUserName: "",  lastUpdateTime: "",  lastUpdatedUserId: "",  lastUpdatedUserName: "",  enabled: "",  deleted: "",   });

  watch(
          () => props.data,
          (newVal: any) => {
            formData.id = ""
            if (newVal?.id) {
              const {  id,  configId,  sort,  checkTypeId,  checkItemId,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  } = newVal;
              Object.assign(formData, {  id,  configId,  sort,  checkTypeId,  checkItemId,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  });
            }
          }
  );

  /** 关闭表单弹窗 */
  function closeDialog() {
    visibleDialog.value = false;
    emit("change-visible", false);
    resetForm();
  }

  /** 重置表单 */
  function resetForm() {
    formRef.value.resetFields();
    formRef.value.clearValidate();
    formData.id = ""
    formData.configId = ""
    formData.sort = ""
    formData.checkTypeId = ""
    formData.checkItemId = ""
    formData.version = ""
    formData.remarks = ""
    formData.createTime = ""
    formData.createdUserId = ""
    formData.createdUserName = ""
    formData.lastUpdateTime = ""
    formData.lastUpdatedUserId = ""
    formData.lastUpdatedUserName = ""
    formData.enabled = ""
    formData.deleted = ""
  }

  const loading = ref(false);

  /** 质量模版配置详情保存提交 */
  function handleSubmit() {
    formRef.value.validate(async (valid: any) => {
      if (valid) {
        loading.value = true;
        const text = formData.id ? "编辑" : "新增";
        await request({
          url: "/qms/qmsreportmodeldetail/save",
          method: "post",
          data: formData,
        }).finally(() => {
          loading.value = false;
        });
        ElMessage.success( text +`成功`);
        closeDialog();
        emit("reset-table", formData.id);
      }
    });
  }


  // 组件销毁时调用
  onBeforeUnmount(() => {
    visibleDialog.value = false;
    emit("change-visible", false);
  });


</script>
